<template>
  <el-dialog
    :title="!flatsForm.flatsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="flatsForm" :rules="dataRule" ref="flatsForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼盘名称" prop="flatsName">
      <el-input v-model="flatsForm.flatsName" placeholder="楼盘名称"></el-input>
    </el-form-item>
    <el-form-item label="楼盘均价" prop="flatsAvgPrice">
      <el-input v-model="flatsForm.flatsAvgPrice" placeholder="楼盘均价"></el-input>
    </el-form-item>
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="flatsForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="flatsForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(县id)" prop="xianId">
      <el-input v-model="flatsForm.xianId" placeholder="地区id(县id)"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="flatsAddress">
      <el-input v-model="flatsForm.flatsAddress" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="开盘时间" prop="starttime">
      <el-input v-model="flatsForm.starttime" placeholder="开盘时间"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="flatsForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="dimension">
      <el-input v-model="flatsForm.dimension" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="装修情况（1 毛坯 2简装 3精装 4豪装）" prop="decolate">
      <el-input v-model="flatsForm.decolate" placeholder="装修情况（1 毛坯 2简装 3精装 4豪装）"></el-input>
    </el-form-item>
    <el-form-item label="销售状态(1 在售 2告罄)" prop="saleStatus">
      <el-input v-model="flatsForm.saleStatus" placeholder="销售状态(1 在售 2告罄)"></el-input>
    </el-form-item>
    <el-form-item label="默认图" prop="images">
      <el-input v-model="flatsForm.images" placeholder="默认图"></el-input>
    </el-form-item>
    <el-form-item label="户型id" prop="unitId">
      <el-input v-model="flatsForm.unitId" placeholder="户型id"></el-input>
    </el-form-item>
    <el-form-item label="小区id" prop="cellId">
      <el-input v-model="flatsForm.cellId" placeholder="小区id"></el-input>
    </el-form-item>
    <el-form-item label="小区名称" prop="cellName">
      <el-input v-model="flatsForm.cellName" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="flatsForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        flatsForm: {
          flatsId: 0,
          flatsName: '',
          flatsAvgPrice: '',
          shenId: '',
          shiId: '',
          xianId: '',
          flatsAddress: '',
          starttime: '',
          longitude: '',
          dimension: '',
          decolate: '',
          saleStatus: '',
          images: '',
          unitId: '',
          cellId: '',
          cellName: '',
          brokerId: ''
        },
        dataRule: {
          flatsName: [
            { required: true, message: '楼盘名称不能为空', trigger: 'blur' }
          ],
          flatsAvgPrice: [
            { required: true, message: '楼盘均价不能为空', trigger: 'blur' }
          ],
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          xianId: [
            { required: true, message: '地区id(县id)不能为空', trigger: 'blur' }
          ],
          flatsAddress: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '开盘时间不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          dimension: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          decolate: [
            { required: true, message: '装修情况（1 毛坯 2简装 3精装 4豪装）不能为空', trigger: 'blur' }
          ],
          saleStatus: [
            { required: true, message: '销售状态(1 在售 2告罄)不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '默认图不能为空', trigger: 'blur' }
          ],
          unitId: [
            { required: true, message: '户型id不能为空', trigger: 'blur' }
          ],
          cellId: [
            { required: true, message: '小区id不能为空', trigger: 'blur' }
          ],
          cellName: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.flatsForm.flatsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['flatsForm'].resetFields()
          if (this.flatsForm.flatsId) {
            this.$http({
              url: this.$http.adornUrl(`/house/flats/info/${this.flatsForm.flatsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.flatsForm.flatsName = data.flats.flatsName
                this.flatsForm.flatsAvgPrice = data.flats.flatsAvgPrice
                this.flatsForm.shenId = data.flats.shenId
                this.flatsForm.shiId = data.flats.shiId
                this.flatsForm.xianId = data.flats.xianId
                this.flatsForm.flatsAddress = data.flats.flatsAddress
                this.flatsForm.starttime = data.flats.starttime
                this.flatsForm.longitude = data.flats.longitude
                this.flatsForm.dimension = data.flats.dimension
                this.flatsForm.decolate = data.flats.decolate
                this.flatsForm.saleStatus = data.flats.saleStatus
                this.flatsForm.images = data.flats.images
                this.flatsForm.unitId = data.flats.unitId
                this.flatsForm.cellId = data.flats.cellId
                this.flatsForm.cellName = data.flats.cellName
                this.flatsForm.brokerId = data.flats.brokerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['flatsForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/flats/${!this.flatsForm.flatsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'flatsId': this.flatsForm.flatsId || undefined,
                'flatsName': this.flatsForm.flatsName,
                'flatsAvgPrice': this.flatsForm.flatsAvgPrice,
                'shenId': this.flatsForm.shenId,
                'shiId': this.flatsForm.shiId,
                'xianId': this.flatsForm.xianId,
                'flatsAddress': this.flatsForm.flatsAddress,
                'starttime': this.flatsForm.starttime,
                'longitude': this.flatsForm.longitude,
                'dimension': this.flatsForm.dimension,
                'decolate': this.flatsForm.decolate,
                'saleStatus': this.flatsForm.saleStatus,
                'images': this.flatsForm.images,
                'unitId': this.flatsForm.unitId,
                'cellId': this.flatsForm.cellId,
                'cellName': this.flatsForm.cellName,
                'brokerId': this.flatsForm.brokerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
